<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
 <head>
  <title>inputEx - Form Usage</title>

<!-- YUI CSS -->
<link rel="stylesheet" type="text/css" href="../lib/yui/reset-fonts/reset-fonts.css">
<link rel="stylesheet" type="text/css" href="../lib/yui/container/assets/container.css"> 
<link rel="stylesheet" type="text/css" href="../lib/yui/assets/skins/sam/skin.css">

<!-- InputEx CSS -->
<link type='text/css' rel='stylesheet' href='../css/inputEx.css' />
<!--[if IE 6]><link type='text/css' rel='stylesheet' href='../css/inputEx-IE6.css' /><![endif]-->

<!-- Demo CSS -->
<link rel="stylesheet" type="text/css" href="css/demo.css"/>
<link rel="stylesheet" type="text/css" href="css/dpSyntaxHighlighter.css">
<style>
div.inputEx-Form-Mask div { background-color:#DDDDFF; }
div#container3 { width:400px }
</style>
 </head>

 <body>

<h1>inputEx - Form Usage</h1>


<!-- Example 1 -->
<div class='exampleDiv'>
   <h2>Basic Form creation</h2>
   <p>Use the following code to create a basic inputEx Form.</p>
   <div class='demoContainer' id='container1'></div>
   <div class='codeContainer'>
      <textarea name="code" class="JScript">
         new inputEx.Form( { 
            fields: [ 
               {type: 'select', label: 'Title', name: 'title', choices: [{ value: 'Mr' }, { value: 'Mrs' }, { value: 'Ms' }] },
               {label: 'Firstname', name: 'firstname', required: true, value:'Jacques' },
               {label: 'Lastname', name: 'lastname', value:'Dupont' },
               {type:'email', label: 'Email', name: 'email'},
               {type:'url', label: 'Website',name:'website'}
            ], 
            buttons: [{type: 'submit', value: 'Change'}],
            parentEl: 'container1'
         });
      </textarea>
   </div>
</div>

<!-- Example 2 -->
<div class='exampleDiv'>
   <h2>Multi-group Form</h2>
   <p>Use the following code to create a Form with multiple Groups (fieldsets).</p>
   <div class='demoContainer' id='container2'></div>
   <div class='codeContainer'>
    <textarea name="code" class="JScript">
         new inputEx.Form( {
              fields: [
                  { 
                     type:'group',
                     legend:'group 1',
                     name:'group1',
                     fields:[
                       {type: 'select', label: 'Title', name: 'title', choices: [{ value: 'Mr' }, { value: 'Mrs' }, { value: 'Ms' }] },
                       {label: 'Firstname', name: 'firstname', required: true, value:'Jacques' },
                       {label: 'Lastname', name: 'lastname', value:'Dupont' }
                     ]
                  },
                  { 
                     type:'group',
                     legend:'group 2',
                     name:'group2',
                     fields:[
                       {type:'email', label: 'Email', name: 'email'},
                       {type:'url', label: 'Website',name:'website'}
                     ]
                  }
              ],
              buttons: [{type: 'submit', value: 'Change'}],
              parentEl: 'container2'
          });
   </textarea>
    </div>
</div>

<!-- Example 3 -->
<div class='exampleDiv'>

   <h2>Send in json with ajax</h2>
   
   <p>How to send the form data using Ajax</p>

   <div class='demoContainer' id='container3'></div>

   <div class='codeContainer'>
      <textarea name="code" class="JScript">
         new inputEx.Form( {
            fields: [ 
               { type: 'select', label: 'Title', name: 'title', choices: [{ value: 'Mr' }, { value: 'Mrs' }, { value: 'Ms' }] },
               { label: 'Firstname', name: 'firstname', required: true, value:'Jacques' },
               { label: 'Lastname', name: 'lastname', value:'Dupont' }
            ], 
            buttons: [{type: 'submit', value: 'Change'}],    
            parentEl: 'container3',
            ajax: {
               method: 'POST',
               uri: 'default.php',
               callback: {
                  success: function(o) { alert("success", o); },
                  failure: function(o) { alert("failure", o); }
               },
               showMask:true
            }
         });
      </textarea>
   </div>

</div>



<!-- Example 4 -->
<div class='exampleDiv'>
   <h2>Send with ajax using url encoded parameters</h2>
   <p>How to send the form data using Ajax</p>
   <div class='demoContainer' id='container4'></div>
   <div class='codeContainer'>
      <textarea name="code" class="JScript">
         new inputEx.Form( {
            fields: [ 
               { type: 'select', label: 'Title', name: 'title', choices: [{ value: 'Mr' }, { value: 'Mrs' }, { value: 'Ms' }] },
               { label: 'Firstname', name: 'firstname', required: true, value:'Jacques' },
               { label: 'Lastname', name: 'lastname', value:'Dupont' }
            ], 
            buttons: [{type: 'submit', value: 'Change'}],    
            parentEl: 'container4',
            ajax: {
               method: 'POST',
               uri: 'default.php',
               contentType: "application/x-www-form-urlencoded",
               callback: {
                  success: function(o) { alert("success", o); },
                  failure: function(o) { alert("failure", o); }
               },
               showMask:true
            }
         });
      </textarea>
   </div>
</div>



<!-- Example 5 -->
<div class='exampleDiv'>
   <h2>Send with ajax using url encoded parameters wrapped in an object</h2>
   <p>Use wrapObject</p>
   <div class='demoContainer' id='container5'></div>
   <div class='codeContainer'>
      <textarea name="code" class="JScript">
         new inputEx.Form( {
            fields: [ 
               { type: 'select', label: 'Title', name: 'title', choices: [{ value: 'Mr' }, { value: 'Mrs' }, { value: 'Ms' }] },
               { label: 'Firstname', name: 'firstname', required: true, value:'Jacques' },
               { label: 'Lastname', name: 'lastname', value:'Dupont' }
            ], 
            buttons: [{type: 'submit', value: 'Change'}],    
            parentEl: 'container5',
            ajax: {
               method: 'POST',
               uri: 'default.php',
               contentType: "application/x-www-form-urlencoded",
               wrapObject: "person",
               callback: {
                  success: function(o) { alert("success", o); },
                  failure: function(o) { alert("failure", o); }
               },
               showMask:true
            }
         });
      </textarea>
   </div>
</div>


<!-- Example 6 -->
<div class='exampleDiv'>
   <h2>Setting and getting form value</h2>
   <p>Use the following code to set or get the value from javascript</p>
   <div class='demoContainer' id='container6'></div>
   <div class='codeContainer'>
      <textarea name="code" class="JScript">
         var form6 = new inputEx.Form( { 
            fields: [ 
               {type: 'select', label: 'Title', name: 'title', choices: [{ value: 'Mr' }, { value: 'Mrs' }, { value: 'Ms' }], value:'Mr' },
               {label: 'Firstname', name: 'firstname', required: true, value:'Jacques' },
               {label: 'Lastname', name: 'lastname', value:'Dupont' },
               {
                  type: 'group',
                  legend: 'Emails',
                  name: 'emails',
                  fields: [
                     {type:'email', label: 'Email 1', name:'first'},
                     {type:'email', label: 'Email 2', name:'second'}
                  ]
               }
            ],
            buttons: [
               {
                  type: 'submit',
                  value: 'Set form value',
                  
                  onClick: function(e) { // e === clickEvent (inputEx.widget.Button custom event)
                     
                     // valueObject : object to be passed to setValue function
                     //               its structure is { field_name : field_value, ... }
                     
                     var valueObject = {
                        title:"Mrs",
                        firstname:"Candy",
                        lastname:"Jones",
                        // note the nested object when setting value of a form with a 'group' field :
                        emails:{
                           first:'first@email.com',
                           second:'second@email.com'
                        }
                     };
                     
                     form6.setValue(valueObject);
                     
                     return false; // stop clickEvent, to prevent form submitting
                     
                  }
               },
               {
                  type: 'submit',
                  value: 'Get form value',
                  
                  onClick:function(e) { // e === clickEvent (inputEx.widget.Button custom event)
                     
                     var valueAsJsObject = form6.getValue();
                     var valueAsJsonString = YAHOO.lang.JSON.stringify(valueAsJsObject);
                     
                     alert(valueAsJsonString);
                     
                     return false; // stop clickEvent, to prevent form submitting
                  }
               }
            ],
            parentEl: 'container6'
         });
      </textarea>
   </div>
</div>

<!-- Example 7 -->
<div class='exampleDiv'>
   <h2>Different button types</h2>
   <p>Use the following code to create submit buttons, or "link" buttons.</p>
   <div class='demoContainer' id='container7'></div>
   <div class='codeContainer'>
      <textarea name="code" class="JScript">
         
         var confirmation = {
            
            message : "Are you sure you want to submit ?",
            
            handler : function() {
               if (!confirm(this.message)) {
                  return false;  // return false to prevent form submit
               }
            }
            
         };
         
         var form7 = new inputEx.Form( { 
            fields: [ 
               {type: 'select', label: 'Title', name: 'title', choices: [{ value: 'Mr' }, { value: 'Mrs' }, { value: 'Ms' }], value:'Mr' },
               {label: 'Firstname', name: 'firstname', required: true, value:'Jacques' },
               {label: 'Lastname', name: 'lastname', value:'Dupont' }
            ], 
            buttons: [
               {type: 'submit', value: 'Submit'},
               {type: 'submit-link', value: 'Submit'},
               {
                  type: 'submit-link',
                  value: 'Confirm and submit',
                  onClick: {
                     fn: confirmation.handler, // function called on click
                     scope : confirmation // will become 'this' inside fn, when fn is called
                  }
               },
               {
                  type: 'link',
                  value: 'Reset form',
                  onClick: function() {form7.clear();} // when scope doesn't matter, simpler syntax to attach a click handler
               }
            ],
            parentEl: 'container7'
         });
      </textarea>
   </div>
   <p>You can use 3 types of inputEx form buttons :
      <ul style="margin-left:40px;list-style-type:disc">
         <li style="list-style-type:disc"><u>submit</u>&nbsp;: a (&lt;input type="submit" /&gt;)-tag is created. A handler is automatically added on 'click' to submit the form only if it validates (no invalid fields, or no empty required fields). </li>
         <li style="list-style-type:disc"><u>submit-link</u>&nbsp;: same as 'submit' type, but with a different markup (&lt;a&gt;&lt;span&gt;Submit&lt;/span&gt;&lt;/a&gt;) to render as 'link'. The same handler as with 'submit' type is added on link.</li>
         <li style="list-style-type:disc"><u>link</u>&nbsp;: same markup as 'submit-link' but with no handler to submit the form. You must add your own 'onClick' function to trigger a specific action. The default link behavior is disabled, so you don't have to do it yourself in the onClick handler.</li>
      </ul>
   </p>
   <br />
   <p>"link" and "submit-link" buttons are also useful because :
      <ul style="margin-left:40px;list-style-type:disc">
         <li style="list-style-type:disc">they allow to differenciate strong actions (like a "submit"), and weak actions (like a form "reset")</li>
         <li style="list-style-type:disc">they are easier to style (see : <a href="styling_buttons.html">styling buttons example</a>)</li>
      </ul>
   </p>
</div>


<!-- Example 8 -->
<div class='exampleDiv'>
   <h2>Destroy a form</h2>
   <p>Remove DOM nodes, remove event listeners, free memory...</p>
   <div class='demoContainer' id='container8'></div>
   <div class='codeContainer'>
      <textarea name="code" class="JScript">
         var form8 = new inputEx.Form( { 
            fields: [ 
               {type: 'select', label: 'Title', name: 'title', choices: [{ value: 'Mr' }, { value: 'Mrs' }, { value: 'Ms' }], value:'Mr' },
               {label: 'Firstname', name: 'firstname', required: true, value:'Jacques' },
               {label: 'Lastname', name: 'lastname', value:'Dupont' }
            ], 
            buttons: [
               {type: 'submit', value: 'Submit'}
            ],
            parentEl: 'container8'
         });
         
         var container = inputEx.cn('div',{id:'destroyButtonContainer'});
         YAHOO.util.Dom.get('container8').appendChild(container);
         
         var destroyButton = new inputEx.widget.Button({
            parentEl: 'destroyButtonContainer',
            id: 'destroyButton',
            type: 'submit',
            value: 'Destroy the form',
            onClick: function() {
               alert('clicked : form will be destroyed');
               form8.destroy(); // remove nodes from DOM, remove event listeners
               delete form8;  // free memory (no references to removed DOM nodes)
            }
         });
         
         container.appendChild(inputEx.cn('div',null,{clear:'both'}));
         
         
      </textarea>
   </div>
</div>


<!-- Example 9 -->
<div class='exampleDiv'>
   <h2>Turn off / on browser autocompletion</h2>
   <p>Activate/deactivate browser autocompletion by field, by form, or for all inputEx fields in the page.</p>
   <div class='demoContainer' id='container9'></div>
   <div class='codeContainer'>
      <textarea name="code" class="JScript">

         // autocompletion is 'on'
         new inputEx.Form( { 
            legend: "Form with autocompletion",
            fields: [ 
               {label: 'Lastname', name: 'lastname', description:'autocomplete option set to true by default' },
               // except on this field
               {type:'email', label: 'Email', name: 'email', autocomplete:false, description:"autocomplete option set to false on this field"}
            ], 
            buttons: [{type: 'submit', value: 'Change'}],
            parentEl: 'container9'
         });

         // autocompletion is 'off' on the whole form
         new inputEx.Form( { 
            legend: "Form without autocompletion",
            autocomplete: false,
            fields: [ 
               {label: 'Lastname', name: 'lastname' },
               {type:'email', label: 'Email', name: 'email'}
            ], 
            buttons: [{type: 'submit', value: 'Change'}],
            parentEl: 'container9'
         });

         // to turn off the browser autocompletion by default in all
         // inputEx fields of the page, set the following value after
         // inputEx source inclusion :
         //
         //   inputEx.browserAutocomplete = false;
         //
      </textarea>
   </div>
</div>

<!-- YUI Library -->
<script type="text/javascript" src="../lib/yui/utilities/utilities.js"></script>
<script type="text/javascript" src="../lib/yui/container/container-min.js"></script>
<script type="text/javascript" src="../lib/yui/json/json-min.js"></script>

<!-- InputEx -->
<script src="../js/inputex.js"  type='text/javascript'></script>
<script src="../js/mixins/choice.js"  type='text/javascript'></script>
<script src="../js/Field.js"  type='text/javascript'></script>
<script src="../js/fields/StringField.js"  type='text/javascript'></script>
<script src="../js/fields/EmailField.js"  type='text/javascript'></script>
<script src="../js/fields/UrlField.js"  type='text/javascript'></script>
<script src="../js/fields/SelectField.js"  type='text/javascript'></script>
<script src="../js/Group.js"  type='text/javascript'></script>
<script src="../js/widgets/Button.js"  type='text/javascript'></script>
<script src="../js/Form.js"  type='text/javascript'></script>

<!-- inputEx examples -->
<script src="js/dpSyntaxHighlighter.js"></script>
<script src="inputex-examples.js"></script>

 </body>
</html>